package client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import java.util.Vector;

public class ProgressUpdate extends JFrame{
	public JProgressBar progress;
	private JPanel mainFrame;
	public JLabel currentTask;
	
	public ProgressUpdate(int lengthOfTask){
		super("Working");
		
		progress = new JProgressBar(JProgressBar.HORIZONTAL, 0, lengthOfTask);
		mainFrame = new JPanel();
		mainFrame.setLayout(new BorderLayout());
		currentTask = new JLabel("Working...");
		mainFrame.setBorder(BorderFactory.createTitledBorder("Progress"));
		mainFrame.add(progress, BorderLayout.NORTH);
		mainFrame.add(currentTask);
		
		setContentPane(mainFrame);
		setResizable(false);
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();		
		setLocation((int)(d.getWidth()/2) - (this.getWidth()/2), (int)(d.getHeight() / 2) - (this.getHeight()/2));
		pack();
		setVisible(true);

	}
	
	public void updateTask(final String newMessage){
		try{
			SwingUtilities.invokeAndWait(new Runnable() {
	            public void run() {
	        		progress.setValue(progress.getValue() + 1);
	        		currentTask.setText(newMessage);
	            }
	          });
		}catch(Exception e){
			e.printStackTrace();
		}
		if(progress.getValue() == progress.getMaximum()){
			JOptionPane.showMessageDialog(this, "Complete");
			this.dispose();
		}
		this.setFocusable(true);
	}
}
